package com.example.androidnote;

import android.Manifest;
import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.os.PersistableBundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.VideoView;

import androidx.annotation.Nullable;
import androidx.core.app.ActivityCompat;
import androidx.core.content.FileProvider;

import com.example.androidnote.Util.DBUtils;
import com.example.androidnote.Util.NoteDB;

import java.io.File;
import java.io.IOException;
import java.security.PrivateKey;

public class AddContent extends Activity implements View.OnClickListener {

    private Button save,cancel,add_img,add_ved;
    private EditText et;
    private ImageView ig;
    private VideoView vv;

    private NoteDB notedb;
    private SQLiteDatabase dbWriter;
    private SQLiteDatabase dbReader;
    private File photo,video;

    private String text = "error";
    private String img_path = "error";
    private String ved_path = "error";
    private String time = "error";
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.note);
        save = findViewById(R.id.save);
        cancel = findViewById(R.id.cancel);
        add_img = findViewById(R.id.add_img);
        add_ved = findViewById(R.id.add_ved);
        et = findViewById(R.id.edt);
        ig = findViewById(R.id.c_img);
        vv = findViewById(R.id.c_ved);
        save.setOnClickListener(this);
        cancel.setOnClickListener(this);
        add_img.setOnClickListener(this);
        add_ved.setOnClickListener(this);
        notedb = new NoteDB(this);
        dbWriter = notedb.getWritableDatabase();
        dbReader = notedb.getReadableDatabase();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.save:{
                addDB();
                finish();
                break;
            }
            case R.id.cancel:{
                finish();
                break;
            }
            case R.id.add_img:{
                Intent iimg = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                iimg.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                photo = new File(Environment.getExternalStorageDirectory().getAbsoluteFile()+"/"+DBUtils.getTimeinE()+".jpg");
                iimg.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photo));
                startActivityForResult(iimg,1);
                break;
            }
            case R.id.add_ved:{
                Intent vi = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
                vi.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                video = new File(Environment.getExternalStorageDirectory().getAbsoluteFile()+"/"+DBUtils.getTimeinE()+".mp4");
                vi.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(video));
                startActivityForResult(vi,2);
                break;
            }
        }
    }


    public void addDB(){
        text = et.getText().toString();
        time = DBUtils.getTime();
        ContentValues cv = new ContentValues();
        cv.put(NoteDB.CONTENT,text);
        cv.put(NoteDB.TIME,time);
        cv.put(NoteDB.IMG_PATH,img_path);
        cv.put(NoteDB.VED_PATH,ved_path);
        dbWriter.insert(NoteDB.TABLE,null,cv);
    }

    @Override
    protected void onActivityResult(int requestCode,int resultCode,Intent data){
        super.onActivityResult(requestCode,resultCode,data);
        if (requestCode == 1){
            Bitmap bitmap = BitmapFactory.decodeFile(photo.getAbsolutePath());
            ig.setVisibility(View.VISIBLE);
            ig.setImageBitmap(bitmap);
            img_path = photo.toString();
        }
        if(requestCode == 2){
            vv.setVisibility(View.VISIBLE);
            vv.setVideoURI(Uri.fromFile(video));
            ved_path = video.toString();
            vv.start();
        }
    }
}
